﻿using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using com.huigo.Framework.BLL;
using com.loto.Common;

namespace com.huigo.Framework.Service
{
    /// <summary>
    /// Summary description for ServiceEntry
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ServiceEntry : System.Web.Services.WebService
    {
        public Credentials token;
        public int Source;

        [WebMethod]
        [SoapHeader("token", Direction = SoapHeaderDirection.In)]
        public string Entry(string req)
        {
            string rtn = string.Empty;
            Source = DictEnum.VerifyPin(token.ACC, token.PIN);
            if (Source > 0)
            {
                Log.Write(LogType.NOR, req);
                return InterceptXMLMethodRequest(req);
                //string JSON = Xml2JSON.XmlToJSON(InterceptXMLMethodRequest(req));
                //return JSON;
            }
            else
            {
                rtn = "err";
            }
            return rtn;
        }

        private string InterceptXMLMethodRequest(string pars)
        {
            string rtn = string.Empty;
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(pars);
                var className = "com.huigo.Framework.Service.ServiceClass";
                Type t = Type.GetType(className);
                XmlNode xmlNode = xmlDocument.SelectSingleNode("//Func");
                var mName = xmlNode.Attributes["value"].Value;
                var m = t.GetMethod(mName);
                var o = Activator.CreateInstance(t);
                var n = t.GetField("Source");
                n.SetValue(o, Source);
                var tt = m.GetParameters();

                var obj = new object[tt.Length];
                for (var i = 0; i < tt.Length; i++)
                {
                    var parameterInfo = tt[i];
                    var tType = parameterInfo.ParameterType;
                    var x = xmlNode.SelectSingleNode(parameterInfo.Name);
                    //obj[i] = Convert.ChangeType(x.InnerText, tType);
                    if (tType.FullName == "System.Byte[]")
                    {
                        obj[i] = Convert.FromBase64String(x.InnerText);
                        //obj[i] = Convert.ChangeType(x.InnerText, tType);
                    }
                    else
                    {
                        obj[i] = Convert.ChangeType(x.InnerText, tType);
                    }
                }
                var r = m.Invoke(o, obj);
                var bb = m.ReturnType;
                if (bb.Equals(typeof(string)))
                {
                    rtn = r.ToString();
                }
            }
            catch (Exception ex)
            {
                Log.Write(LogType.ERR, ex.Message + "||" + pars);
            }
            return rtn;
        }
    }
}
